/**
Copyright [2008] [Julian Gamble] 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 
	
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
 **/
package aerith.swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JToolBar;

public class AerithHeader extends JToolBar {
    /**
	 * 
	 */
	private static final long serialVersionUID = 2607520749811948990L;

	////////////////////////////////////////////////////////////////////////////
    // THEME SPECIFIC FIELDS
    ////////////////////////////////////////////////////////////////////////////
    private Color lightColor = Color.decode("#4B5461");

    private Color shadowColor = Color.decode("#202737");

    private int preferredHeight = 32;

    private BufferedImage backgroundGradient;

    public AerithHeader() {
    	super();
    	backgroundGradient = loadImage("/swing/header-gradient.png");
    	setFloatable(false);
    }
    
    private BufferedImage loadImage(String resourcePath) {
    	java.net.URL imageurl = this.getClass().getResource(resourcePath);
    	BufferedImage result = null;
    	try {
			result =  javax.imageio.ImageIO.read(imageurl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
    }
    
    @Override
    public Dimension getPreferredSize() {
        Dimension size = super.getPreferredSize();
        size.height = preferredHeight;
        return size;
    }
    
    @Override
    public Dimension getMaximumSize() {
        Dimension size = super.getMaximumSize();
        size.height = preferredHeight;
        return size;
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        if (!isVisible()) {
            return;
        }
        
        Graphics2D g2 = (Graphics2D) g;
        paintBackground(g2);
    }

    private void paintBackground(final Graphics2D g2) {
        int height = backgroundGradient.getHeight();
        
        Rectangle bounds = g2.getClipBounds();
        g2.drawImage(backgroundGradient,
                     (int) bounds.getX(), 0,
                     (int) bounds.getWidth(), height,
                     null);
        
        g2.setColor(lightColor);
        g2.drawLine(0, height, getWidth(), height);
        
        g2.setColor(shadowColor);
        g2.drawLine(0, height + 1, getWidth(), height + 1);
    }
}